package com.zb.secure;

import cn.dev33.satoken.secure.SaSecureUtil;
import cn.dev33.satoken.util.SaFoxUtil;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.codec.Base64Decoder;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.digest.MD5;
import cn.hutool.crypto.symmetric.AES;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.UUID;

/**
 * @Author WEN
 * @Date 2023/7/8 0008
 **/
public class BCryptUtil {
    /**
     * 生成盐值
     * @return
     */
    public static String generatorSalt() {
        return SaSecureUtil.sha256(UUID.randomUUID().toString());
    }

    /**
     * 加密密码
     * @param password
     * @param salt
     * @return
     */
    public static String encryPassword(String password, String salt) {
        return SaSecureUtil.sha256(SaSecureUtil.md5BySalt(password, salt));
    }

    /**
     * 公钥加密数据
     * @param publicKey 公钥
     * @param data 数据明文
     * @return
     */
    public static String rsaEncry(String publicKey, String data) {
        return SaSecureUtil.rsaEncryptByPublic(publicKey, data);
    }

    /**
     * 私钥解密
     * @param privateKey 私钥
     * @param encryData 公钥加密后的数据密文
     * @return
     */
    public static String rsaDecry(String privateKey, String encryData) {
        return SaSecureUtil.rsaDecryptByPrivate(privateKey, encryData);
    }

    /**
     * AES加密
     * @param key 加密密钥
     * @param data 明文数据
     * @return
     */
    public static String aesEncry(String key, String data) {
        String aesEncrypt = ZbSecureUtil.aesEncrypt(key, data);
        return base64Encode(aesEncrypt);
    }

    /**
     * AES解密
     * @param key 加密的密钥
     * @param encryData 密文
     * @return
     */
    public static String aesDecry(String key, String encryData) {
        String encode = base64Decode(encryData);
        return ZbSecureUtil.aesDecrypt(key, encode);
    }

    /**
     * Base64编码
     * @param data
     * @return
     */
    public static String base64Encode(String data) {
        String encode = Base64.encode(data, "UTF-8");
        return encode;
    }

    /**
     * Base64编码
     * @param data
     * @return
     */
    public static String base64Decode(String data) {
        String decode = Base64.decodeStr(data, "UTF-8");
        return decode;
    }

    /**
     * MD5加密
     * @param data 明文
     * @return
     */
    public static String MD5(String data) {
        return SaSecureUtil.md5(data);
    }



    public static void main(String[] args) throws Exception {
        String data = "1895334855135785548555===&&&&&&";
        String randomString = SaFoxUtil.getRandomString(256);
        System.out.println(randomString);
        String encrypt = aesEncry(randomString, data);
        System.out.println(encrypt);
        String decrypt = aesDecry(randomString, encrypt);
        System.out.println(decrypt);

    }
}
